import time
from selenium import webdriver
from selenium.webdriver.common.by import By

# 定义一个driver的变量，用来接收实例化后的浏览器
driver = webdriver.Chrome()
# 使用get方法，访问网址
driver.get('https://www.baidu.com/')

# 常用元素定位方式
# By.ID
# By.LINK_TEXT
# By.CLASS_NAME
# By.PARTIAL_LINK TEXT
# By.TAG_NAME
# By.CSS_SELECTOR
driver.find_element(By.CSS_SELECTOR, 'div#s-top-left>div:nth-child(3)')
driver.find_elements(By.CSS_SELECTOR, 'div#s-top-left>div')[3]
# By.NAME
driver.find_element(By.NAME, 'wd')
# By.XPATH
# 绝对路径
driver.find_element(By.XPATH, '/html/body/div/div/div[3]/a')
# 相对路径
driver.find_element(By.XPATH, '//input[@id="kw"]')
driver.find_element(By.XPATH, '//input[@id="s-top-left"]/a[4]')
# 父级
driver.find_element(By.XPATH, '//div/div/..')
# 文本匹配
driver.find_element(By.XPATH, '//span[text(="文本内文字"]')
# 文本包含
driver.find_element(By.XPATH, '//span[contains(text),"所包含的文字"')
# 同级弟弟
driver.find_element(By.XPATH, '//div[@id=="id"]/following-sibling::div')
# 同级哥哥
driver.find_element(By.XPATH, '//div[@id=="id"]/preceding-sibling::div')
